package com.aruba.bulethoothdemoapplication.utils.printer;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by xuqiwei on 17-5-3.
 */

public class DataConvertUtils {
    public static String integer2Str(Integer integer) {
        String str = "-1";
        if (integer != null) {
            str = integer.toString();
        }
        return str;
    }

    public static Integer str2Integer(String str) {
        if (str == null || str.equals("")) {
            return 0;
        }
        Integer integer = null;
        try {
            integer = Integer.parseInt(str);
        } catch (NumberFormatException nfe) {
            nfe.printStackTrace();
        }
        return integer;

    }

    public static String removeZero(String str) {
        if (str == null || "null".equals(str)) {
            return "";
        }
        if (str.contains(".0")) {
            return str.replace(".0", "");
        } else {
            return str;
        }
    }

    public static boolean isPupilReason(String str) {
        if (str == null || "null".equals(str)) {
            return false;
        }
        if (str.contains("无法观察")) {
            return true;
        } else {
            return false;
        }
    }

    public static String nullCheck(String originalData) {
        if (originalData == null || "null".equals(originalData)) {
            return "";
        } else {
            return originalData;
        }

    }

    public static boolean isTextNotNullOrEmpty(String text) {
        if (text == null || "null".equals(text) || "".equals(text)) {
            return false;
        } else {
            return true;
        }

    }

    public static Integer intNullCheck(Integer integer) {
        try {
            if (integer == null) {
                return 0;
            } else {
                return integer;
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            return 0;
        }
    }

    public static <T> List<T> castList(Class<T> clz, List list) {
        if (list == null || list.size() == 0) {
            return new ArrayList<>();
        }
        List<T> res = new ArrayList<>();
        for (Object o : list) {
            res.add(clz.cast(o));
        }
        return res;
    }

}
